package com.tzy.collection.controller;


import com.tzy.collection.service.MyCollectionService;
import com.tzy.common.constant.WebResult;
import com.tzy.common.constant.WebResultMsgCode;
import com.tzy.common.controller.BaseController;
import com.tzy.common.entity.Dishes;
import com.tzy.common.entity.MyCollection;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 我的收藏表 前端控制器
 * </p>
 *
 * @author 姣姣同学
 * @since 2022-03-01
 */
@RestController
//前端页面路径
@RequestMapping("/collection")
@Slf4j
public class MyCollectionController extends BaseController {
    @Resource
    private MyCollectionService myCollectionService;
    /**
     *根据菜品编号，商家编号，评价编号来查询收藏列表
     * @return 返回固定的json数据格式
     */
    @GetMapping("/list")
    public WebResult getList() {
        List<Map<String, Object>> list = myCollectionService.getList();
        return new WebResult(WebResultMsgCode.SUCCESS).putData("list",list);
    }

    /**
     * 根据收藏id来查询全部
     * @param
     * @return
     */
    @GetMapping("/list/userId")
    public WebResult getListById(HttpServletRequest request){
        int userId = getUserId(request);
        log.info("userId={}",userId);
        List<Map<String, Object>> list = myCollectionService.getListByUserId(userId);
        return new WebResult(WebResultMsgCode.SUCCESS).putData("list",list);
    }
    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public WebResult removeById(int id){
        log.info("id:"+id);
        log.info("ID={}",id);
        MyCollection myCollection = new MyCollection();
        myCollection.setId(id);
        myCollection.setIsDelete(1);
        boolean updateById =myCollectionService.updateById(myCollection);
        if(updateById){
            return new WebResult(WebResultMsgCode.SUCCESS);
        }
        return new WebResult(WebResultMsgCode.FAIL,"删除失败");
    }

    /**
     * 插入菜品收藏页面的数据
     * @param
     * @return
     */
    @PostMapping("/save")
    public WebResult insert(int userId,int dishesId, int shopId,
           int evaluationId,
            Float distance){
        MyCollection myCollection = new MyCollection();
        myCollection.setDistance(distance);
        myCollection.setDishesId(dishesId);
        myCollection.setEvaluationId(evaluationId);
        myCollection.setShopId(shopId);
        myCollection.setUserId(userId);
        boolean save = myCollectionService.save(myCollection);
        if(save){
            return new WebResult(WebResultMsgCode.SUCCESS,true);
        }else {
            return new WebResult(WebResultMsgCode.SUCCESS,false);
        }

    }
}
